//
//  BDFillBusinessInfoSuccessViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/13.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDFillBusinessInfoSuccessViewController.h"
#import "BDGetShopInfoRequest.h"
#import "BQActivityView.h"
#import "BDUserShopModel.h"
#import "BDBindQrcodeRequest.h"
#import "HSYScanQRCodeViewController.h"
#import "BDBindQrSuccessViewController.h"


@interface BDFillBusinessInfoSuccessViewController ()

@property (weak, nonatomic) IBOutlet UILabel *shopFullNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *accountNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *shopOwnerLabel;

@end

@implementation BDFillBusinessInfoSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.shopFullNameLabel.text = self.model.name;
    self.accountNameLabel.text = self.model.contactCellphone;
    self.shopOwnerLabel.text = self.model.shortName;
    
    [self getShopInfo];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getShopInfo{
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];
    
    NSString *uid = [NSString stringWithFormat:@"%ld",(long)self.model.uid];

    BDGetShopInfoRequest *task = [[BDGetShopInfoRequest alloc] init];
    task.uid = uid;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];
        
        if (responseObject) {
            BDUserShopModel *model = [BDUserShopModel mj_objectWithKeyValues:responseObject];
            weakSelf.shopFullNameLabel.text = model.appBizShop.name;
            weakSelf.accountNameLabel.text = model.appAuUser.cellphone;
            weakSelf.shopOwnerLabel.text = model.appBizShop.shortName;

        }else{
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];
}



- (IBAction)bingQRCodeBtnClcik:(id)sender {
    
    HSYScanQRCodeViewController *vc = [[HSYScanQRCodeViewController alloc] init];
    __weak typeof(self)weakSelf = self;
    
    vc.scanResultBlock = ^(NSDictionary * info){
        if (info[@"code"]) {
            [weakSelf bindQRcode:info[@"code"] shopid:weakSelf.model];
        }else{
            [weakSelf showMessage:info[@"resultString"]];
        }
    };
    
    HSYBaseNavigationController *nav = [HSYBaseNavigationController navigtationControllerWithRootViewController:vc];
    
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (void)bindQRcode:(NSString *)code shopid:(BDBusinessModel *)model{
    
    NSString *uid = [NSString stringWithFormat:@"%ld",(long)self.model.uid];
    NSString *shopid = [NSString stringWithFormat:@"%ld",(long)self.model.shopId];
    
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];

    BDBindQrcodeRequest *task = [[BDBindQrcodeRequest alloc] init];
    task.code = code;
    task.uid = uid;
    task.shopId = shopid;
    
    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];

        if (responseObject) {
            BDUserShopModel *model = [BDUserShopModel mj_objectWithKeyValues:responseObject];
            [weakSelf showBindSuccessWithUserShopModel:model];
        }else{
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];
}

- (void)showBindSuccessWithUserShopModel:(BDUserShopModel *)model{
    BDBindQrSuccessViewController *vc = [[BDBindQrSuccessViewController alloc] init];
    vc.model = model;
    
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
